package com.xyht.sca_s.student_manage_system.modules.org.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.org.entity.req.*;
import com.xyht.sca_s.student_manage_system.modules.org.service.SmsOrgStructureService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 组织架构 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2022-11-27
 */
@RestController
@RequestMapping("/org")
@SecretBody
public class SmsOrgStructureController {
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private SmsOrgStructureService smsOrgStructureService;

    @ApiModelProperty(value = "通讯录批量导入")
    @PostMapping("/user/batch/add")
    public ResponseResult batchAddUserInfo(@RequestBody Map<String,Object> map, HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try{
            Future<ResponseResult> future = smsOrgStructureService.batchAddUserInfo(map,user_id);
            while (true){
                if (future.isDone()){
                    commonCodeEnum = future.get();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiModelProperty(value = "通讯录批量删除")
    @PostMapping("/user/batch/delete")
    public ResponseResult batchDeleteUserInfo(@RequestBody Map<String,Object> map){
        return smsOrgStructureService.batchDeleteUserInfo(map);
    }

    @ApiModelProperty(value = "通讯录获取用户信息")
    @GetMapping("/user/get")
    public ResponseResult getOrgUserInfo(@RequestParam("flag") Integer flag,
                                         @RequestParam(value = "org_id",required = false) Integer org_id,
                                         @RequestParam(value = "search_str",required = false) String search_str,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
                                         ){
        return smsOrgStructureService.getOrgUserInfo(org_id,flag,search_str,pageSize,pageNum);
    }

    @ApiModelProperty(value = "通讯录获取用户详情")
    @GetMapping("/user/get/detail")
    public ResponseResult getOrgUserDetail(@RequestParam("user_id") String user_id){
        return smsOrgStructureService.getOrgUserDetail(user_id);
    }

    @ApiModelProperty(value = "通讯录用户角色详情修改")
    @PostMapping("/user/update/detail")
    public ResponseResult updateUserDetail(@RequestBody OrgUserDetailReq orgUserDetailReq,HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsOrgStructureService.updateUserDetail(orgUserDetailReq,user_id);
    }

    @ApiModelProperty(value = "获取组织树")
    @GetMapping("/getOrgTree")
    public ResponseResult getOrgTree(@RequestParam(value = "level",required = false) String level){
        return smsOrgStructureService.getOrgTree(level);
    }

    @ApiModelProperty(value = "获取学院组织树")
    @GetMapping("/collageTree")
    public ResponseResult getOrgCollageTree(){
        return smsOrgStructureService.getOrgCollageTree();
    }

    @ApiModelProperty(value = "组织详情")
    @GetMapping("/id")
    public ResponseResult getOrgByID(@RequestParam("id")Integer id){
        return smsOrgStructureService.getOrgByID(id);
    }

    @ApiModelProperty(value = "根组织列表")
    @GetMapping("/rootOrgList")
    public ResponseResult getRootOrgList(){
        return smsOrgStructureService.getRootOrgList();
    }

    @ApiModelProperty(value = "添加组织")
    @PostMapping("/add")
    public ResponseResult addOrg(@RequestBody SmsOrgStructureAddReq smsOrgStructureAddReq){
        return smsOrgStructureService.addOrg(smsOrgStructureAddReq);
    }

    @ApiModelProperty(value = "修改组织")
    @PostMapping("/update")
    public ResponseResult updateOrg(@RequestBody SmsOrgStructureUpdateReq smsOrgStructureUpdateReq){
        return smsOrgStructureService.updateOrg(smsOrgStructureUpdateReq);
    }

    @ApiModelProperty(value = "删除组织")
    @PostMapping("/delete")
    public ResponseResult deleteOrg(@RequestBody SmsOrgStructureUpdateReq req){
        return smsOrgStructureService.deleteOrg(req.getId());
    }

    @ApiModelProperty(value = "获取指定人(审批表相关)")
    @GetMapping("/getAppointUser")
    public ResponseResult getProcessUserInfo( @RequestParam("processObjectType") Integer processObjectType,
                                              @RequestParam("processObjectId") String processObjectId){
        return smsOrgStructureService.getAppointUser(processObjectType,processObjectId);
    }


    @ApiOperation(value = "添加组织负责人")
    @PostMapping("/addOrgLeader")
    public ResponseResult addOrgLeader(@RequestBody SmsOrgStructureLeaderReq addLeaderReq){
        return smsOrgStructureService.addOrgLeader(addLeaderReq);
    }

    @ApiOperation(value = "获取组织负责人")
    @GetMapping("/getOrgLeader")
    public ResponseResult getOrgLeader(@RequestParam(value = "orgId") Integer orgId){
        return smsOrgStructureService.getOrgLeader(orgId);
    }

    @ApiOperation(value = "删除组织负责人")
    @PostMapping("/deletedOrgLeader")
    public ResponseResult deletedOrgLeader(@RequestBody SmsOrgStructureLeaderReq deletedLeaderReq){
        return smsOrgStructureService.deletedOrgLeader(deletedLeaderReq);
    }

    @ApiOperation(value = "获取用户部门")
    @GetMapping("/departmentList")
    public ResponseResult getOrgLeader(@RequestParam("user_id") String user_id){
        return smsOrgStructureService.getUserDepartmentList(user_id);
    }

    //查level为1的根组织
    @ApiOperation(value = "获取level为1的根组织")
    @GetMapping("/getOneLevelOrgList")
    public ResponseResult getOneLevelOrgList(){
        return smsOrgStructureService.getOneLevelOrgList();
    }
}

